package com;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBC {
	// 定义连接数据库的信息
	public static final String URL = "jdbc:mysql://10.131.8.253:3306/student42";
	public static final String USERNAME = "student42";
	public static final String PASSWORD = "123456";

	//public static final String URL = "jdbc:mysql://localhost:3306/shop";
	//public static final String USERNAME = "root";
	//public static final String PASSWORD = "123456";

	// 0. 加载驱动
	static {
		try {
//				Class.forName("com.mysql.jdbc.cj.Driver");
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// 1. 连接数据库
	public static Connection getConnection() throws SQLException {
		Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
		return conn;
	}

	// 2. 释放资源
	public static void destory(Connection conn, Statement stmt, ResultSet rs) {
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (stmt != null) {
			try {
				stmt.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
